﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LongestFlags.Question;

namespace LongestFlags.Answer
{
    public class Libby : ILongestFlag
    {
        public int GetLongestFlags(int[] iArray, out int startIndex, out int endIndex)
        {
            Dictionary<int, int> storage = new Dictionary<int, int>();
            startIndex = 0;
            endIndex = 0;
            if (iArray.Length == 0)
            {
                return 0;
            }
            int currentMax = 0;


            int delta = 0;
            storage.Add(0, -1);
            for (int i = 0; i < iArray.Length; i++)
            {
                if (iArray[i] < 0)
                {
                    delta++;
                }
                else if (iArray[i] > 0)
                {
                    delta--;
                }
                //If equals 0, nothing need to do.

                if (storage.ContainsKey(delta))
                {
                    if (currentMax < i - storage[delta])
                    {
                        startIndex = storage[delta] + 1;
                        endIndex = i;
                        currentMax = i - storage[delta];
                    }
                }
                else
                {
                    storage.Add(delta, i);
                }
            }
            return currentMax;
        }



    }
}
